package com.bw.ppjokeplay.mvvm.view

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager

/**
 * @author Lishaohe
 * @description:
 * @date :2021/11/17 0017 下午 7:50
 *
 */
class ConnectionReceiver(networkChangeListener: OnNetWorkChangeListener) :BroadcastReceiver(){
    var networkChangeListener :OnNetWorkChangeListener ?= networkChangeListener
//广播接收者
    override fun onReceive(context: Context?, intent: Intent?) {

        if (ConnectivityManager.CONNECTIVITY_ACTION == intent?.action){
            // 获取连接管理器对象
            val connectivityManager : ConnectivityManager = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            //Android判断当前网络状态是否连接
            val activeNetworkInfo = connectivityManager.activeNetworkInfo
            //获取当前网络连接的类型信息
            if (activeNetworkInfo == null || !activeNetworkInfo.isAvailable) {
                networkChangeListener?.onNetworkChange()
            }
        }

    }
}